package com.vlong.town.controller.admin.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.activity.ActivityInfo;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.service.activity.ActivityInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 文化活动信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/activity")
public class ActivityInfoController extends BaseController {

    // Fields

    @Autowired
    private ActivityInfoService activityInfoService;// 文化活动信息业务实现类

    /**
     * 进入文化活动信息列表页面
     * @return activity/list_activity
     */
    @RequestMapping("/list")
    public String gotoActivityInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc,
            @RequestParam(required=false, value="translate") String translate
    		) {
		 Integer  t=null;
	     if(translate!=null&&!translate.equals("-1")){
	     	t = Integer.parseInt(translate);
	     }
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<ActivityInfo> activityInfoList = this.activityInfoService.queryList(title, content, desc,t);
        PageInfo<ActivityInfo> pageInfo = new PageInfo<ActivityInfo>(activityInfoList);
        PageTag<ActivityInfo> pt = new PageTag<ActivityInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        request.setAttribute("translate", translate);
        for(ActivityInfo activityInfo : activityInfoList) {
            activityInfo.setContent(DataUtil.delHTMLTag(activityInfo.getContent(), 0));
        }
        request.setAttribute("activityInfoList", activityInfoList);
        return "admin/activity/list_activity";
    }

    /**
     * 进入文化活动信息添加页面
     * @return activity/add_activity
     */
    @GetMapping("/addActivity")
    public String gotoActivityInofAddPage() {
        return "activity/add_activity";
    }

    /**
     * 进入文化活动信息修改页面
     * @return activity/edit_activity
     */
    @GetMapping("/editActivity/{activityId}")
    public String gotoActivityInofEditPage(@PathVariable(required=true, value="activityId") Long activityId) {
        ActivityInfo activityInfo = this.activityInfoService.queryById(activityId);
        request.setAttribute("activityInfo", activityInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + activityInfo.getThumbnail());
        return "admin/activity/edit_activity";
    }

    /**
     * 添加文化活动信息
     * @param title 文化活动标题
     * @param content 文化活动内容
     * @param desc 文化活动简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddActivity")
    public DataMessage addActivity(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("文化活动标题不合法");
            return dm;
        }
        this.activityInfoService.add(1L, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("文化活动添加成功");
        return dm;
    }

    /**
     * 修改文化活动信息
     * @param title 文化活动标题
     * @param content 文化活动内容
     * @param desc 文化活动简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditActivity")
    public DataMessage editActivity(
            @RequestParam(name="activityId", required=true) Long activityId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="translate", required=false) String translate,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("文化活动标题不合法");
            return dm;
        }
        
        Integer  t=null;
	     if(translate!=null&&!translate.equals("-1")){
	     	t = Integer.parseInt(translate);
	     }
        this.activityInfoService.update(activityId, title, content, desc, picture,t);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("文化活动修改成功");
        return dm;
    }

    /**
     * 删除文化活动
     * @param activityId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="activityId") Long activityId) {
        DataMessage dm = new DataMessage();
        if(activityId != null && activityId > 0L) {
            this.activityInfoService.delete(activityId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("文化活动删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除文化活动
     * @param activityIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="activityIds") String activityIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(activityIds, 1, 255)) {
            String[] activityIdArray = activityIds.split(",");
            for(String activityId : activityIdArray) {
                this.activityInfoService.delete(Long.parseLong(activityId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("文化活动删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
